package ru.pb.game.network.engine.client.packets.receivable;

import ru.pb.game.network.engine.client.packets.FromClientToGameServerPacket;
import ru.pb.game.network.engine.client.packets.sendable.PROTOCOL_BATTLE_ENDBATTLE_ACK;
import ru.pb.game.network.engine.client.packets.sendable.opcode_3078_ACK;
import ru.pb.game.objects.model.players.Player;
import ru.pb.game.objects.model.room.Room;
import ru.pb.game.objects.storage.ObjectsStorage;

/**
 * Скока время осталос
 * 
 * @author Felixx
 */
public class PROTOCOL_BATTLE_TIMERSYNC_REQ extends FromClientToGameServerPacket
{
	private int _timeLost, unkD, unkC1, unkC2;

	@Override
	public void readImpl()
	{
		readH();

		_timeLost = readD();
		unkD = readD();
		unkC1 = readC();
		unkC2 = readC();

		// _log.info("TIME LOST:" + _timeLost);
		// _log.info("UNK_D:" + unkD);// 1065353216 почимуто
		// _log.info("UNK_C1:" + unkC1);// 0 почимуто
		// _log.info("UNK_C2:" + unkC2); // 1 почимуто
	}

	@Override
	public void runImpl()
	{
		Player pl = getPlayer();
		if(getPlayer() != null)
		{
			Room r = pl.getRoom();
			r.setTimeLost(_timeLost);
			if(_timeLost < 1)
			{
				Player member = null;
				for(int objId : r.getAllPlayers().toArray())
				{
					member = ObjectsStorage.getInstance().getPlayer(objId);
					member.sendPacket(new opcode_3078_ACK());
					member.sendPacket(new PROTOCOL_BATTLE_ENDBATTLE_ACK(member));
				}
			}
		}
	}
}